﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SOEI.Solution.Application.Contracts.CommonOthers.Pdf;
using SOEI.Solution.Application.Contracts.CommonOthers.Pdf.Dtos;
using SOEI.Solution.CommonOthers.Pdf.DomainService;
using SOEI.Solution.CommonOthers.Pdf.StructEntities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using SOEI.Solution.Helper;

namespace SOEI.Solution.CommonOthers.Pdf
{
    [ApiExplorerSettings(GroupName = "Manager", IgnoreApi = false)]
    public class PdfAppService : SolutionAppServiceBase, IPdfAppService
    {
        public IPdfManager _manager;

        public PdfAppService(IPdfManager manager)
        {
            _manager = manager;
        }

        /// <summary>根据HTML创建PDF
        /// </summary>
        public async Task<string> Create(PdfInputDto input)
        {
            var vParam = ObjectMapper.Map<PdfParam>(input);

            return await _manager.Create(vParam);
        }

        /// <summary>获取PDF内容
        /// </summary>
        public async Task<FileContentResult> Get(PdfInputDto input)
        {
            var vParam = ObjectMapper.Map<PdfParam>(input);

            byte[] vData = await _manager.GetPdfAsync(vParam);

            Helper.HttpContext.Current.Response.Headers.Remove("X-Frame-Options");

            return new FileContentResult(vData, "application/pdf");
        }

        /// <summary>判断是否存PDF文件
        /// </summary>
        public async Task<bool> IsExistsPdf(PdfInputDto input)
        {
            var vParam = ObjectMapper.Map<PdfParam>(input);

            return await _manager.IsExistsPdf(vParam);
        }
    }
}
